<template>
  <div class='container'>
    <!-- 表单 -->
    <el-row type="flex" class="row-bg" justify="end">
      <el-col :span="22"><div class="grid-content bg-purple">
          <el-form :inline="true" :model="formData" class="demo-form-inline" ref="form">
          <el-form-item label="目录名称" prop="directoryName">
            <el-input v-model="formData.directoryName"></el-input>
          </el-form-item>
          <el-form-item label="状态" prop="state" style="padding-left:50px">
            <el-select v-model="formData.state"  placeholder="请选择">
              <el-option label="启用" value="1"></el-option>
              <el-option label="禁用" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="doPurge">清除</el-button>
            <el-button type="primary" @click="doSearch">搜索</el-button>
          </el-form-item>
        </el-form>
        </div></el-col>
      <el-col :span="2"><div class="grid-content bg-purple" style="display:flex;justify-content: flex-end;">
        <el-button type="text" v-if="$route.query.id" icon="el-icon-back" @click="$router.push({path:'/subjects/list'})">返回学科</el-button>
        <el-button type="success" icon="el-icon-edit" @click="addSubject">新增目录</el-button>
        </div></el-col>
    </el-row>

<!-- 消息提示 -->
   <el-alert
   :title="`数据一共 ${total} 条`"
    type="info"
    show-icon
    :closable="false"
    >
  </el-alert>

  <!-- table表格 -->
     <el-table
      :data="directorysData"
      style="width: 100%">
         <el-table-column
          prop="indexId"
        label="序号"
        >
      </el-table-column>
      <el-table-column
        prop="subjectName"
        label="所属学科"
        width="203px">
      </el-table-column>
      <el-table-column
        prop="directoryName"
        label="目录名称"
        width="203px">
      </el-table-column>
      <el-table-column
        prop="username"
        label="创建者"
        width="203px">
      </el-table-column>
       <el-table-column
        prop="addDate"
        label="创建日期"
        width="203px">
          <template v-slot={row}>
              {{new Date(row.addDate).toLocaleString().replaceAll('/','-')}}
          </template>
      </el-table-column>
      <el-table-column
        prop="totals"
        label="面试题数量"
        width="203px">
      </el-table-column>
       <el-table-column
        prop="state"
        label="状态"
        width="203px">
          <template v-slot={row}>
            {{ row.state ? "已启用" :  "已禁用"  }}
          </template>
      </el-table-column>
       <el-table-column
        label="操作"
        width="150px">
        <template v-slot={row}>
          <!-- <div v-if="row.state===1"> -->
            <el-button type="text" @click="buttonChange(row)">{{row.state?"启用":"禁用"}}</el-button>
            <el-button type="text" :disabled="row.state" @click="doEdit(row.id)">修改</el-button>
            <el-button type="text" :disabled="row.state" @click="doDelete(row.id)">删除</el-button>
          <!-- </div> -->
           <!-- <div v-else>
            <el-button type="text" @click="buttonChange(row)">启用</el-button>
            <el-button type="text" disabled>修改</el-button>
            <el-button type="text" disabled>删除</el-button>
          </div> -->
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
     <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-size="formData.pagesize"
      :page-sizes="[5, 10, 20,50]"
      :current-page="formData.page"
      layout="prev, pager, next, sizes,jumper"
      background
      :total="total"
     >
    </el-pagination>

    <!-- 对话框 -->
    <directorysAdd
    :dialogFormVisible.sync="dialogFormVisible"
    @dataUpdate="getDirectorysData"
    :isEditId="isEditId"
    :isEdit.sync="isEdit"
    >
    </directorysAdd>
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/directorys.js'
import directorysAdd from '@/module-hmmm/components/directorys-add.vue'
export default {
  name: 'directorys',
  data () {
    return {
      directorysData: [],
      formData: {
        page: 1,
        pagesize: 10,
        subjectID: null,
        directoryName: null,
        state: null
      },
      total: null,
      dialogFormVisible: false,
      isEdit: '',
      isEditId: null
    }
  },
  created () {
    console.log(this.$route.query.id)
    if (this.$route.query.id) {
      this.toBack = true
      this.formData.subjectID = this.$route.query.id
    }
    // console.log(111)
    this.getDirectorysData()
  },
  components: {
    directorysAdd
  },
  methods: {
    // 目录列表
    async getDirectorysData () {
      const { data } = await list(this.formData)
      this.directorysData = data.items
      this.directorysData.forEach((item, index) => {
        item.indexId = index + 1
      })
      this.total = data.counts
    },
    // 每页数据大小变化
    handleSizeChange (val) {
      this.formData.pagesize = val
      this.getDirectorysData()
    },
    // 页码变化
    handleCurrentChange (val) {
      this.formData.page = val
      this.getDirectorysData()
    },
    // 按钮禁用变化
    async buttonChange (row) {
      if (row.state) {
        row.state = 0
      } else {
        row.state = 1
      }
      await changeState({ id: row.id, state: row.state })
      this.$message({
        message: '操作成功',
        type: 'success'
      })
    },
    // 删除
    async doDelete (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove({ id })
        if (this.directorysData.length === 1) {
          this.formData.page = 1
        }
        this.getDirectorysData()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      })
    },
    // 清除按钮
    doPurge () {
      this.$refs.form.resetFields()
    },
    // 搜索
    doSearch () {
      this.getDirectorysData()
    },
    // 修改
    doEdit (id) {
      this.isEditId = id
      this.isEdit = true
      this.dialogFormVisible = true
    },
    // 新增
    addSubject () {
      this.isEdit = false
      this.dialogFormVisible = true
    }
  }
}

</script>

<style scoped lang='scss'>
.container{
  margin: 10px;
  padding: 20px;
  background-color: #ffffff;
  .el-table{
    margin-top: 20px;
    margin-bottom: 20px;
  }
  .el-pagination{
    display: flex;
    justify-content: flex-end;
  }
}
</style>
